package beans;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Random;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

@ManagedBean
@SessionScoped
public class UserNumberBean {

	Integer randomInt = null;
	Integer userNumber = null;
	int userN;
	
	
	String response = null;

	private long maximum = 10;
	private long minimum = 0;

	private static Map<String, Object> countries;
	static {
		countries = new LinkedHashMap<String, Object>();
		countries.put("English", new Locale("en")); // label, value
		countries.put("Romana", new Locale("ro"));
	}

	public UserNumberBean() {

		Random randomGR = new Random();
		randomInt = new Integer(randomGR.nextInt(10));
		System.out.println("Nr lui Duke este : " + randomInt);
	}

	public void getNewRandom() {

		Random randomGR = new Random();
		this.randomInt = new Integer(randomGR.nextInt(10));

	}

	public Integer getUserNumber() {
		return userNumber;
	}

	public void setUserNumber(Integer userNumber) {
		this.userNumber = userNumber;
	}

	public String getResponse() {
		if ((userNumber != null) && (userNumber.compareTo(randomInt) == 0)) {

			userN = userNumber;
			getNewRandom();

			return "success";

		}
		if (userNumber == null) {

			return "fail";
		} else {
			userN = userNumber;
			setUserNumber(null);
			return "fail";

		}
	}

	public long getMaximum() {
		return maximum;
	}

	public void setMaximum(long maximum) {
		this.maximum = maximum;
	}

	public long getMinimum() {
		return minimum;
	}

	public void setMinimum(long minimum) {
		this.minimum = minimum;
	}

	public Map<String, Object> getCountriesInMap() {
		return countries;
	}

	// value change event listener
	public void countryLocaleCodeChanged(ValueChangeEvent e) {

		String newLocaleValue = e.getNewValue().toString();

		// loop country map to compare the locale code
		for (Map.Entry<String, Object> entry : countries.entrySet()) {

			if (entry.getValue().toString().equals(newLocaleValue)) {

				FacesContext.getCurrentInstance().getViewRoot()
						.setLocale((Locale) entry.getValue());

			}
			FacesContext.getCurrentInstance().renderResponse();
		}
	}

}
